package cn.xiaoniu.day05.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import cn.xiaoniu.day05.bean.User;
import cn.xiaoniu.day05.dao.UserDao;
import cn.xiaoniu.day05.util.DBUtil;

public class UserDaoImpl implements UserDao{

	@Override
	public boolean save(User user) {
		QueryRunner runner = DBUtil.getRunner();
		String sql =" insert into user values (?,?,?,?,?)";
		try {
			runner.update(sql,null,user.getName(),user.getPasswd(),user.getAge(),user.getPnum());
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public User getUserByName(String name) {
		QueryRunner runner = DBUtil.getRunner();
		String sql = "select * from user where name=?";//user   唯一
		try {
			User user = runner.query(sql, new BeanHandler<User>(User.class),name);
			return user;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
